package cn.lik.airTycoon;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;

import cn.lik.airTycoon.http.AirTycoonHttp;
import cn.lik.airTycoon.http.bean.AirTycoonBean;

public class RunMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Properties properties = new Properties();
			properties.load(new FileInputStream(AirTycoonHttp.class.getClassLoader().getResource("air.properties").getFile()));

			AirTycoonBean bean;
			long time = System.currentTimeMillis();
			int bucks = 0;
			int times = 0;
			while (true) {
				times++;
				bean = getUpHttp(properties).result();
				while (bean.getLevel() < 42) {
					bean = getUpHttp(properties).result();
				}
				while (bean.getLevel() > 41) {
					bean = getDownHttp(properties).result();
				}
				if ((times % 100) == 0) {
					// if ((System.currentTimeMillis() - time) > 20000) {
					System.out.println("times:" + times + "  " + (System.currentTimeMillis() - time) / 1000 + "s:" + (bean.getBucks() - bucks) + "  All:" + bean.getBucks());
					time = System.currentTimeMillis();
					bucks = bean.getBucks();
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static AirTycoonHttp getUpHttp(Properties properties) {
		AirTycoonHttp http = AirTycoonHttp.iniAirTycoonHttp("http://aws-flight.anansimobile.org:8996/api/updateplayer");
		http.addHeader(AirTycoonHttp.SESSION, properties.getProperty("session"));
		http.addHeader(AirTycoonHttp.CHECKSUM, properties.getProperty("checksum"));
		http.addDatas(properties.getProperty("toUp"));
		return http;
	}

	public static AirTycoonHttp getDownHttp(Properties properties) {
		AirTycoonHttp http = AirTycoonHttp.iniAirTycoonHttp("http://aws-flight.anansimobile.org:8996/api/updateplayer");
		http.addHeader(AirTycoonHttp.SESSION, properties.getProperty("session"));
		http.addHeader(AirTycoonHttp.CHECKSUM, properties.getProperty("checksum"));
		http.addDatas(properties.getProperty("toDown"));
		return http;
	}

}
